package com.qf.web.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.aop.aspect.ApiOperationLog;
import com.qf.aop.aspect.ApiOperationLogTitle;
import com.qf.api.blog.dto.WikiDTO;
import com.qf.api.blog.entity.WikiEntity;
import com.qf.api.blog.service.WikiCatalogService;
import com.qf.api.blog.service.WikiService;
import com.qf.api.blog.vo.WikiCatalogVO;
import com.qf.api.blog.vo.WikiPageVO;
import com.qf.api.sys.mybatis.page.BaseController;
import com.qf.api.sys.mybatis.page.PageResult;
import com.qf.core.enums.common.BusinessTypeEnum;
import com.qf.core.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: sin
 * @Date: 2025/5/25 10:14
 * @Description: 知识库
 **/
@RestController
@ApiOperationLogTitle("知识库模块")
@RequestMapping("/admin/wiki")
@Tag(name = "02.知识库管理")
public class AdminWikiController extends BaseController {

    @Resource
    private WikiService wikiService;

    @Resource
    private WikiCatalogService wikiCatalogService;

    /**
     * title 模糊查询 (like)
     **/
    @Operation(summary = "获取知识库分页")
    @GetMapping
    public ResponseResult<PageResult<WikiPageVO>> getWikiPage(@RequestParam(name = "title",required = false) String title,
                                                              @RequestParam(name = "isTop",required = false) Integer isTop,
                                                              @RequestParam(name = "isPublish",required = false) Integer isPublish) {
        startPage();
        List<WikiEntity> list = wikiService.list(
                new LambdaQueryWrapper<WikiEntity>()
                        .like(title != null && !title.isEmpty(), WikiEntity::getTitle, title)
                        .eq(isTop != null, WikiEntity::getIsTop, isTop)
                        .eq(isPublish != null, WikiEntity::getIsPublish, isPublish).orderByDesc(WikiEntity::getCreateTime)
        );

        return ResponseResult.success(getData(BeanUtil.copyToList(list, WikiPageVO.class)));
    }

    /**
     * 通过文章Id获取单个知识库
     **/
    @GetMapping("/{id}")
    @Operation(summary = "获取单个知识库")
    public ResponseResult<WikiDTO> getArticleById(@PathVariable Long id){
        return ResponseResult.success(wikiService.getArticleById(id));
    }

    @Operation(summary = "新增知识库")
    @ApiOperationLog(desc = "新增知识库",businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseResult<Boolean> save(@RequestBody @Valid WikiDTO wikiDTO) {
        return isOk(wikiService.addWiki(wikiDTO));
    }

    @Operation(summary = "修改知识库")
    @ApiOperationLog(desc = "新增知识库",businessType = BusinessTypeEnum.MODIFY)
    @PutMapping("/{id}")
    public ResponseResult<Boolean> editWiki(@PathVariable("id") Long id, @RequestBody @Valid WikiDTO wikiDTO) {
        return isOk(wikiService.editWiki(id, wikiDTO));
    }

    @Operation(summary = "删除知识库",description = "批量删除（List<Long> ids）逻辑删除 deleted")
    @ApiOperationLog(desc = "删除知识库",businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping
    public ResponseResult<Boolean> delWiki(@RequestBody List<Long> ids) {
        return isOk(wikiService.removeBatchByIds(ids));
    }

    /**
     * id: 知识库ID
     * type: 修改类型     0 -- 修改置顶  1 -- 修改发布
     * value: 状态值
     **/
    @PutMapping("/{id}/{type}/{value}")
    @Operation(summary = "更新知识库 置顶/发布")
    @ApiOperationLog(desc = "更新知识库 置顶/发布",businessType = BusinessTypeEnum.MODIFY)
    public ResponseResult<Boolean> updateWikiTopOrPublish(@PathVariable @NotNull Long id,
                                                       @PathVariable @NotNull Integer type,
                                                       @PathVariable @NotNull Integer value){
        return isOk(wikiService.updateWikiTopOrPublish(id, type, value));
    }

    @Operation(summary = "判断title是否已经存在")
    @GetMapping("/check")
    public ResponseResult<Boolean> checkWikiTitle(@RequestParam("title") String title) {
        return ResponseResult.success(wikiService.checkWikiTitle(title));
    }

    @Operation(summary = "获取知识库目录",description = "根据知识库Id获取对应的目录")
    @GetMapping("/catalog/{id}")
    public ResponseResult<List<WikiCatalogVO>> getWikiCatalog(@PathVariable("id") @NotNull Long wikiId) {
        return ResponseResult.success(wikiCatalogService.getWikiCatalog(wikiId));
    }
}
